package Controller.admin;

import java.io.File;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import Bean.MusicBean;
import Dao.adminDao;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class uploadController implements Controller{
	private adminDao dao;
	
	public void setDao(adminDao dao) {
		this.dao = dao;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		int postMaxSize = 10 * 1024 * 1024;
		String albumPath = "/media/albumArt/";
		String audioPath = "/media/audio/";
		String videoPath = "/media/video/";
		String realAlbumPath = request.getSession().getServletContext().getRealPath(albumPath);
		String realAudioPath = request.getSession().getServletContext().getRealPath(audioPath);
		String realVideoPath = request.getSession().getServletContext().getRealPath(videoPath);
		String encoding = "UTF-8";
		
		MultipartRequest albumRequest = new MultipartRequest(request, realAlbumPath, postMaxSize, encoding, new DefaultFileRenamePolicy());
		//MultipartRequest audioRequest = new MultipartRequest(request, realAudioPath, postMaxSize, encoding, new DefaultFileRenamePolicy());
		//MultipartRequest videoRequest = new MultipartRequest(request, realVideoPath, postMaxSize, encoding, new DefaultFileRenamePolicy());
		MusicBean bean = new MusicBean();
		String album = albumRequest.getFilesystemName("albumArt");
       // String audio = audioRequest.getFilesystemName("audio");
       // String video = videoRequest.getFilesystemName("video");
        
        bean.setMs_s_no(Integer.parseInt(albumRequest.getParameter("s_no")));
        bean.setMs_title(albumRequest.getParameter("title"));
        String year = albumRequest.getParameter("year");
        String month = albumRequest.getParameter("month");
        String day = albumRequest.getParameter("day");
        
        bean.setMs_rd(Integer.parseInt(year+month+day));
        bean.setMs_featuring(albumRequest.getParameter("feat"));
        bean.setMs_genre(albumRequest.getParameter("genre"));
        bean.setMs_album(albumRequest.getParameter("album"));
        
        System.out.println(bean.getMs_album()+" : "+bean.getMs_featuring()+" : "+bean.getMs_rd()
        		+" : "+bean.getMs_genre()+" : "+bean.getMs_title()+" : "+bean.getMs_s_no());
        
        
        dao.insertMusic(bean);
        
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/mypageForm.mb");
		return mav;
	}
}
